<!DOCTYPE HTML>

<html>
    <head>
        <script type="application/ld+json">
    {
        "@context" : "http://schema.org",
        "@type" : "BlogPosting",
        "mainEntityOfPage": {
             "@type": "WebPage",
             "@id": "\/blog\/"
        },
        "articleSection" : "blog",
        "name" : "商品表的设计和商品添加业务",
        "headline" : "商品表的设计和商品添加业务",
        "description" : "mff的个人博客",
        "inLanguage" : "en",
        "author" : "",
        "creator" : "",
        "publisher": "",
        "accountablePerson" : "",
        "copyrightHolder" : "",
        "copyrightYear" : "2020",
        "datePublished": "2020-04-20 00:00:00 \x2b0000 UTC",
        "dateModified" : "2020-04-20 00:00:00 \x2b0000 UTC",
        "url" : "\/blog\/blog\/%E5%95%86%E5%93%81%E8%A1%A8%E7%9A%84%E8%AE%BE%E8%AE%A1%E5%92%8C%E6%B7%BB%E5%8A%A0%E5%95%86%E5%93%81%E4%B8%9A%E5%8A%A1\/",
        "wordCount" : "604",
        "keywords" : [ "tutorial","Blog" ]
    }
    </script>
        
            
                <title>商品表的设计和商品添加业务</title>
            
        

        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="generator" content="Hugo 0.69.0" />
        
  
    
  

  

  <link rel="apple-touch-icon-precomposed" href='/blog/favicon/apple-touch-icon-precomposed.png'>
  <link rel="icon" href='/blog/favicon/favicon.png'>
  
  <meta name="msapplication-TileColor" content="#da532c">
  <meta name="msapplication-TileImage" content='/favicon/mstile.png'>
  <meta name="application-name" content="mff blog">
  <meta name="msapplication-tooltip" content="">
  <meta name="msapplication-config" content='/favicon/ieconfig.xml'>



        
            <meta name="author" content="mff">
        
        
            <meta name="description" content="mff的个人博客">
        

        <meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="商品表的设计和商品添加业务"/>
<meta name="twitter:description" content="mff的个人博客"/>

        <meta property="og:title" content="商品表的设计和商品添加业务" />
<meta property="og:description" content="mff的个人博客" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/blog/blog/%E5%95%86%E5%93%81%E8%A1%A8%E7%9A%84%E8%AE%BE%E8%AE%A1%E5%92%8C%E6%B7%BB%E5%8A%A0%E5%95%86%E5%93%81%E4%B8%9A%E5%8A%A1/" />
<meta property="article:published_time" content="2020-04-20T00:00:00+00:00" />
<meta property="article:modified_time" content="2020-04-20T00:00:00+00:00" />

        <meta property="og:image" content="/blog//images/logo.png">
        <meta property="og:image:type" content="image/png">
        <meta property="og:image:width" content="512">
        <meta property="og:image:height" content="512">
        <meta itemprop="name" content="商品表的设计和商品添加业务">
<meta itemprop="description" content="mff的个人博客">
<meta itemprop="datePublished" content="2020-04-20T00:00:00&#43;00:00" />
<meta itemprop="dateModified" content="2020-04-20T00:00:00&#43;00:00" />
<meta itemprop="wordCount" content="604">



<meta itemprop="keywords" content="tutorial," />
        

        
            
        

        
        
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/atom-one-light.min.css">
            <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway:400,800,900|Source+Sans+Pro:400,700">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.css">
            <link rel="stylesheet" href="/blog/css/main.css">
            <link rel="stylesheet" href="/blog/css/add-on.css">
            <link rel="stylesheet" href="/blog/css/academicons.min.css">
        

        
            
                
            
        


  
    
    <link href='//cdn.bootcss.com/highlight.js/9.11.0/styles/github.min.css' rel='stylesheet' type='text/css' />
  


      
    </head>
    <body>

      
      <div id="wrapper">

    
    
<header id="header">
    
      <h1><a href="/blog/">blog</a></h1>
    

    <nav class="links">
        <ul>
            
                <li>
                    <a href="/blog/">
                            <i class="fa fa-home">&nbsp;</i>主页
                    </a>
                </li>
            
                <li>
                    <a href="/blog/blog/">
                            <i class="fa fa-newspaper-o">&nbsp;</i>Blog笔记
                    </a>
                </li>
            
                <li>
                    <a href="https://mff-project.gitee.io/hugoblog/posts/%E4%B8%AA%E4%BA%BA%E7%AE%80%E5%8E%86/">
                            <i class="fa fa-id-card-o">&nbsp;</i>个人简历
                    </a>
                </li>
            
                <li>
                    <a href="/blog/itemized/">
                            <i class="fa fa-list">&nbsp;</i>最近项目
                    </a>
                </li>
            
                <li>
                    <a href="/blog/contact/">
                            <i class="fa fa-envelope-o">&nbsp;</i>联系我
                    </a>
                </li>
            
        </ul>
    </nav>
    <nav class="main">
        <ul>
            
            <li id="share-nav" class="share-menu" style="display:none;">
                <a class="fa-share-alt" href="#share-menu">Share</a>
            </li>
            
            <li class="search">
                <a class="fa-search" href="#search">Search</a>
                <form id="search" method="get" action="//google.com/search">
                    <input type="text" name="q" placeholder="Search" />
                    <input type="hidden" name="as_sitesearch" value="/blog/">
                </form>
            </li>
            <li class="menu">
                <a class="fa-bars" href="#menu">Menu</a>
            </li>
        </ul>
    </nav>
</header>


<section id="menu">

    
        <section>
            <form class="search" method="get" action="//google.com/search">
                <input type="text" name="q" placeholder="Search" />
                <input type="hidden" name="as_sitesearch" value="/blog/">
            </form>
        </section>

    
        <section>
            <ul class="links">
                
                    <li>
                        <a href="/blog/">
                            <h3>
                                <i class="fa fa-home">&nbsp;</i>主页
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/blog/">
                            <h3>
                                <i class="fa fa-newspaper-o">&nbsp;</i>Blog笔记
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="https://mff-project.gitee.io/hugoblog/posts/%E4%B8%AA%E4%BA%BA%E7%AE%80%E5%8E%86/">
                            <h3>
                                <i class="fa fa-id-card-o">&nbsp;</i>个人简历
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/itemized/">
                            <h3>
                                <i class="fa fa-list">&nbsp;</i>最近项目
                            </h3>
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/contact/">
                            <h3>
                                <i class="fa fa-envelope-o">&nbsp;</i>联系我
                            </h3>
                        </a>
                    </li>
                
            </ul>
        </section>

    
        <section class="recent-posts">
            <div class="mini-posts">
                <header>
                    <h3>Recent Posts</h3>
                </header>
                

                
                    
                

                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/fastdfs%E9%85%8D%E7%BD%AE%E5%8F%8A%E4%BB%8B%E7%BB%8D/">FastDFS配置及介绍</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-08-27'>
                                    August 27, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/orm%E5%B8%B8%E8%A7%84%E6%93%8D%E4%BD%9C%E9%87%8D%E7%82%B9/">orm常规操作</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-28'>
                                    July 28, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/%E6%94%AF%E4%BB%98%E5%AE%9D%E6%B2%99%E7%AE%B1%E6%94%AF%E4%BB%98%E6%93%8D%E4%BD%9C/">支付宝沙箱支付操作</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-28'>
                                    July 28, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/%E8%85%BE%E8%AE%AF%E7%9F%AD%E4%BF%A1%E5%8F%91%E9%80%81%E9%AA%8C%E8%AF%81%E7%A0%81/">腾讯短信发送验证码</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-28'>
                                    July 28, 2020</time>
                            </header>
                            

                        </article>
                
                        <article class="mini-post">
                            <header>
                                <h3><a href="/blog/blog/flask&#43;vue%E5%AE%9E%E7%8E%B0%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB/">flask&#43;vue实现前后端分离</a></h3>
                                
                                <time class="published" datetime=
                                    '2020-07-26'>
                                    July 26, 2020</time>
                            </header>
                            

                        </article>
                

                
                    <a href=
                        
                            /blog/
                        
                        class="button">View more posts</a>
                
            </div>
        </section>

    
        
</section>

    <section id="share-menu">
    <section id="social-share-nav">
        <ul class="links">
            <header>
                <h3>Share this post <i class="fa fa-smile-o"></i></h3>
            </header>
            



<li>
  <a href="//twitter.com/share?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;text=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1&amp;via=" target="_blank" class="share-btn twitter">
    <i class="fa fa-twitter"></i>
    <p>Twitter</p>
    </a>
</li>




<li>
  <a href="//plus.google.com/share?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f" target="_blank" class="share-btn google-plus">
    <i class="fa fa-google-plus"></i>
    <p>Google+</p>
  </a>
</li>





<li>
  <a href="//www.facebook.com/sharer/sharer.php?u=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f" target="_blank" class="share-btn facebook">
    <i class="fa fa-facebook"></i>
    <p>Facebook</p>
    </a>
</li>




<li>
  <a href="//reddit.com/submit?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;title=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1" target="_blank" class="share-btn reddit">
    <i class="fa fa-reddit-alien"></i>
    <p>Reddit</p>
  </a>
</li>




<li>
  <a href="//www.linkedin.com/shareArticle?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;title=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1" target="_blank" class="share-btn linkedin">
      <i class="fa fa-linkedin"></i>
      <p>LinkedIn</p>
    </a>
</li>




<li>
  <a href="//www.stumbleupon.com/submit?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;title=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1" target="_blank" class="share-btn stumbleupon">
    <i class="fa fa-stumbleupon"></i>
    <p>StumbleUpon</p>
  </a>
</li>




<li>
  <a href="//www.pinterest.com/pin/create/button/?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;description=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1" target="_blank" class="share-btn pinterest">
    <i class="fa fa-pinterest-p"></i>
    <p>Pinterest</p>
  </a>
</li>




<li>
  <a href="mailto:?subject=Check out this post by mff&amp;body=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f" target="_blank" class="share-btn email">
    <i class="fa fa-envelope"></i>
    <p>Email</p>
  </a>
</li>


        </ul>
    </section>
</section>

    
    <div id="main">
        
        
        <article class="post">
  <header>
    <div class="title">
        
            <h1><a href="/blog/blog/%E5%95%86%E5%93%81%E8%A1%A8%E7%9A%84%E8%AE%BE%E8%AE%A1%E5%92%8C%E6%B7%BB%E5%8A%A0%E5%95%86%E5%93%81%E4%B8%9A%E5%8A%A1/">商品表的设计和商品添加业务</a></h1>
            
        
        
            <p>mff的个人博客</p>
        
    </div>
    <div class="meta">
        

        <time class="published"
            datetime='2020-04-20'>
            April 20, 2020</time>
        <span class="author">mff</span>
        
            <p>3 minute read</p>
        
        
    </div>
</header>


  
    <section id="social-share">
      <ul class="icons">
        



<li>
  <a href="//twitter.com/share?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;text=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1&amp;via=" target="_blank" class="share-btn twitter">
    <i class="fa fa-twitter"></i>
    <p>Twitter</p>
    </a>
</li>




<li>
  <a href="//plus.google.com/share?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f" target="_blank" class="share-btn google-plus">
    <i class="fa fa-google-plus"></i>
    <p>Google+</p>
  </a>
</li>





<li>
  <a href="//www.facebook.com/sharer/sharer.php?u=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f" target="_blank" class="share-btn facebook">
    <i class="fa fa-facebook"></i>
    <p>Facebook</p>
    </a>
</li>




<li>
  <a href="//reddit.com/submit?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;title=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1" target="_blank" class="share-btn reddit">
    <i class="fa fa-reddit-alien"></i>
    <p>Reddit</p>
  </a>
</li>




<li>
  <a href="//www.linkedin.com/shareArticle?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;title=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1" target="_blank" class="share-btn linkedin">
      <i class="fa fa-linkedin"></i>
      <p>LinkedIn</p>
    </a>
</li>




<li>
  <a href="//www.stumbleupon.com/submit?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;title=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1" target="_blank" class="share-btn stumbleupon">
    <i class="fa fa-stumbleupon"></i>
    <p>StumbleUpon</p>
  </a>
</li>




<li>
  <a href="//www.pinterest.com/pin/create/button/?url=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f&amp;description=%e5%95%86%e5%93%81%e8%a1%a8%e7%9a%84%e8%ae%be%e8%ae%a1%e5%92%8c%e5%95%86%e5%93%81%e6%b7%bb%e5%8a%a0%e4%b8%9a%e5%8a%a1" target="_blank" class="share-btn pinterest">
    <i class="fa fa-pinterest-p"></i>
    <p>Pinterest</p>
  </a>
</li>




<li>
  <a href="mailto:?subject=Check out this post by mff&amp;body=%2fblog%2fblog%2f%25E5%2595%2586%25E5%2593%2581%25E8%25A1%25A8%25E7%259A%2584%25E8%25AE%25BE%25E8%25AE%25A1%25E5%2592%258C%25E6%25B7%25BB%25E5%258A%25A0%25E5%2595%2586%25E5%2593%2581%25E4%25B8%259A%25E5%258A%25A1%2f" target="_blank" class="share-btn email">
    <i class="fa fa-envelope"></i>
    <p>Email</p>
  </a>
</li>


      </ul>
    </section>
  

  

  <div id="content">
    <h3 id="设计图">设计图</h3>
<p><img src="/blog/img/imgs/%E5%95%86%E5%93%81%E8%A1%A8%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE.jpg" alt="商品表思维导图"></p>
<p>当商品存为单表时，新增一种商品类别就得新建一张表，上万种商品类就得建上万张表，虽然能提供良好的检索服务，但使很难维护</p>
<p>而右边图将所有商品以json形式存储到一张表里，易维护，但是检索服务比较繁琐，这也是唯一的缺点</p>
<p>建立一对多关系模型</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">from</span> django.db <span style="color:#f92672">import</span> models
<span style="color:#75715e"># 导入时间域</span>
<span style="color:#f92672">from</span> django.utils <span style="color:#f92672">import</span> timezone

<span style="color:#75715e"># 基类</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Base</span>(models<span style="color:#f92672">.</span>Model):
    <span style="color:#75715e">#时间字段</span>
    create_time <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>DateTimeField(default<span style="color:#f92672">=</span>timezone<span style="color:#f92672">.</span>now,null<span style="color:#f92672">=</span>True)

    <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Meta</span>:
        abstract <span style="color:#f92672">=</span> True
<span style="color:#75715e"># 商品表</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Goods</span>(Base):
    <span style="color:#75715e"># 名称</span>
    name <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>)
    <span style="color:#75715e"># 商品描述</span>
    desc <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>,null<span style="color:#f92672">=</span>True)
    <span style="color:#75715e"># 图片</span>
    img <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>)
    <span style="color:#75715e"># 展示视频</span>
    video <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>,null<span style="color:#f92672">=</span>True)
    <span style="color:#75715e"># 价格</span>
    price <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>IntegerField()
    <span style="color:#75715e"># 规格</span>
    params <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">400</span>)
    <span style="color:#75715e"># 关注数</span>
    flows <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>IntegerField(default<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>,null<span style="color:#f92672">=</span>True)
    <span style="color:#75715e"># 外键关系(这里的外键可以理解为逻辑外键，可以实现外键的功能，但不会受到外键的约束)</span>
    cid <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>IntegerField(null<span style="color:#f92672">=</span>True)


    <span style="color:#75715e"># 声明表名</span>
    <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Meta</span>:
        db_table <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;goods&#34;</span>

<span style="color:#75715e"># 商品类表</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Category</span>(Base):
    name <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>CharField(max_length<span style="color:#f92672">=</span><span style="color:#ae81ff">200</span>,null<span style="color:#f92672">=</span>True)

        <span style="color:#75715e"># 声明表名</span>
    <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Meta</span>:
        db_table <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;category&#34;</span>
</code></pre></div><p>执行迁移命令生成数据表</p>
<p>新建序列化操作文件(myser.py)</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#导包</span>
<span style="color:#f92672">from</span> rest_framework <span style="color:#f92672">import</span> serializers

<span style="color:#75715e"># 导入需要序列化的表</span>
<span style="color:#f92672">from</span> myapp.models <span style="color:#f92672">import</span> Goods,Category

<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">GoodsSer</span>(serializers<span style="color:#f92672">.</span>ModelSerializer):

    <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Meta</span>:
        model <span style="color:#f92672">=</span> Goods
        fields <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;__all__&#34;</span>
        
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CategorySer</span>(serializers<span style="color:#f92672">.</span>ModelSerializer):

    <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Meta</span>:
        model <span style="color:#f92672">=</span> Category
        fields <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;__all__&#34;</span>
</code></pre></div><p>新建商品(goods.py)文件</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">from</span> rest_framework.views <span style="color:#f92672">import</span> APIView
<span style="color:#f92672">from</span> myapp.models <span style="color:#f92672">import</span> Goods,Category
<span style="color:#f92672">from</span> myapp.myser <span style="color:#f92672">import</span> GoodsSer,CategorySer
<span style="color:#f92672">from</span> rest_framework.response <span style="color:#f92672">import</span> Response

<span style="color:#75715e"># 商品分类</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">CategoryList</span>(APIView):
    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get</span>(self,request):
        
        category <span style="color:#f92672">=</span> Category<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>all()

        category_ser <span style="color:#f92672">=</span> CategorySer(category,many<span style="color:#f92672">=</span>True)

        <span style="color:#66d9ef">return</span> Response(category_ser<span style="color:#f92672">.</span>data)
    
    
<span style="color:#75715e"># 商品入库</span>
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">InsertGoods</span>(APIView):
    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">get</span>(self,requests):
        <span style="color:#75715e"># 接参</span>
        name <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;name&#39;</span>,None)
        price <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;price&#39;</span>,None)
        <span style="color:#75715e"># 这里接收数据为数组，以字符串的方式进行存储</span>
        params <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;param&#39;</span>,None)
        img <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;img&#39;</span>,None)
        video <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;video&#39;</span>,None)
        selected <span style="color:#f92672">=</span> requests<span style="color:#f92672">.</span>GET<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;selected&#39;</span>,None)

        <span style="color:#75715e"># 排重操作</span>
        goods <span style="color:#f92672">=</span> Goods<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(name<span style="color:#f92672">=</span>name)<span style="color:#f92672">.</span>first()

        <span style="color:#66d9ef">if</span> goods:

            <span style="color:#66d9ef">return</span> Response({<span style="color:#e6db74">&#39;code&#39;</span>:<span style="color:#ae81ff">403</span>,<span style="color:#e6db74">&#34;message&#34;</span>:<span style="color:#e6db74">&#34;您以添加过商品&#34;</span>})

        <span style="color:#75715e"># 查询商品分类id</span>
        category <span style="color:#f92672">=</span> Category<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(name<span style="color:#f92672">=</span>selected)<span style="color:#f92672">.</span>first()

        <span style="color:#75715e"># 入库</span>
        goods <span style="color:#f92672">=</span> Goods(name<span style="color:#f92672">=</span>name,price<span style="color:#f92672">=</span>price,params<span style="color:#f92672">=</span>params,img<span style="color:#f92672">=</span>img,cid<span style="color:#f92672">=</span>category<span style="color:#f92672">.</span>id,video<span style="color:#f92672">=</span>video)

        goods<span style="color:#f92672">.</span>save()
        
        <span style="color:#66d9ef">return</span> Response({<span style="color:#e6db74">&#39;code&#39;</span>:<span style="color:#ae81ff">200</span>,<span style="color:#e6db74">&#34;message&#34;</span>:<span style="color:#e6db74">&#34;添加成功&#34;</span>})
</code></pre></div><p>这里是一个添加分类和商品的接口，记得配好接口路由</p>
<p>对应前端(vue.js)做相应操作</p>
<p>新建添加商品(addgoods.vue)文件</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vue" data-lang="vue">&lt;<span style="color:#f92672">template</span>&gt;
    &lt;<span style="color:#f92672">div</span>&gt;
        &lt;<span style="color:#f92672">myheader</span>&gt;&lt;/<span style="color:#f92672">myheader</span>&gt;

        &lt;<span style="color:#f92672">section</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;featured-block text-center&#34;</span>&gt;
            &lt;<span style="color:#f92672">div</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;container&#34;</span>&gt;
                &lt;<span style="color:#f92672">div</span>&gt;
                    &lt;<span style="color:#f92672">Breadcrumb</span> <span style="color:#f92672">:datas</span><span style="color:#e6db74">=&#34;datas&#34;</span>&gt;&lt;/<span style="color:#f92672">Breadcrumb</span>&gt;
                &lt;/<span style="color:#f92672">div</span>&gt;
            &lt;/<span style="color:#f92672">div</span>&gt;
        &lt;/<span style="color:#f92672">section</span>&gt;
        
        &lt;<span style="color:#f92672">div</span>&gt;
            &lt;<span style="color:#f92672">table</span>&gt;
                &lt;<span style="color:#f92672">tr</span>&gt;
                    &lt;<span style="color:#f92672">td</span>&gt;
                        <span style="color:#a6e22e">商品分类</span><span style="color:#960050;background-color:#1e0010">：</span>
                    &lt;/<span style="color:#f92672">td</span>&gt;
                    &lt;<span style="color:#f92672">td</span>&gt;
                        &lt;<span style="color:#f92672">Select</span> <span style="color:#f92672">v-model</span><span style="color:#e6db74">=&#34;selected&#34; </span><span style="color:#f92672">:datas</span><span style="color:#e6db74">=&#34;category&#34;</span>&gt;&lt;/<span style="color:#f92672">Select</span>&gt;
                    &lt;/<span style="color:#f92672">td</span>&gt;
                &lt;/<span style="color:#f92672">tr</span>&gt;
                 &lt;<span style="color:#f92672">tr</span>&gt;
                    &lt;<span style="color:#f92672">td</span>&gt;
                        <span style="color:#a6e22e">商品名称</span><span style="color:#960050;background-color:#1e0010">：</span>
                    &lt;/<span style="color:#f92672">td</span>&gt;
                    &lt;<span style="color:#f92672">td</span>&gt;
                        &lt;<span style="color:#f92672">input</span> <span style="color:#a6e22e">type</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;text&#34;</span> <span style="color:#f92672">v-model</span><span style="color:#e6db74">=&#34;name&#34; placeholder=&#34;请输入商品名称&#34;&gt;
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                &lt;/tr&gt;
</span><span style="color:#e6db74">                &lt;tr&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        商品价格：
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        &lt;input type=&#34;number&#34; v-model=&#34;price&#34; placeholder=0&gt;
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                &lt;/tr&gt;
</span><span style="color:#e6db74">                &lt;tr&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        颜色：
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        &lt;input type=&#34;text&#34; v-model=&#34;color&#34; placeholder=&#34;请输入商品颜色类&#34;&gt;
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                &lt;/tr&gt;
</span><span style="color:#e6db74">                &lt;tr&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        尺寸：
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        &lt;input type=&#34;text&#34; v-model=&#34;size&#34; placeholder=&#34;请输入商品尺寸&#34;&gt;
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                &lt;/tr&gt;
</span><span style="color:#e6db74">                &lt;tr&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        季节：
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        &lt;input type=&#34;text&#34; v-model=&#34;season&#34; placeholder=&#34;请输入季节类&#34;&gt;
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                &lt;/tr&gt;
</span><span style="color:#e6db74">                &lt;tr&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        上传商品图片：
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        &lt;input type=&#34;file&#34; @change=&#34;upload_qiniu&#34;&gt;
</span><span style="color:#e6db74">                        &lt;br&gt;
</span><span style="color:#e6db74">                        {{ load_percent }}
</span><span style="color:#e6db74">                        &lt;!-- 进度条标签 --&gt;
</span><span style="color:#e6db74">                        &lt;Progress :percent=&#34;load_int&#34; color=&#34;green&#34; v-show=&#34;load_int&#34;&gt;						&lt;/Progress&gt;
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                &lt;/tr&gt;
</span><span style="color:#e6db74">                &lt;tr&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        展示视频：
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        &lt;input type=&#34;file&#34; @change=&#34;upload_qiniu&#34;&gt;
</span><span style="color:#e6db74">                        &lt;br&gt;
</span><span style="color:#e6db74">                        {{ load_percent }}
</span><span style="color:#e6db74">                        &lt;Progress :percent=&#34;load_int&#34; color=&#34;green&#34; v-show=&#34;load_int&#34;&gt;						&lt;/Progress&gt;
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                &lt;/tr&gt;
</span><span style="color:#e6db74">                &lt;tr&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;&lt;/td&gt;
</span><span style="color:#e6db74">                    &lt;td&gt;
</span><span style="color:#e6db74">                        &lt;button @click=&#34;submit&#34;&gt;确定上传&lt;/button&gt;
</span><span style="color:#e6db74">                    &lt;/td&gt;
</span><span style="color:#e6db74">                &lt;/tr&gt;
</span><span style="color:#e6db74">            &lt;/table&gt;
</span><span style="color:#e6db74">        &lt;/div&gt;
</span><span style="color:#e6db74">        &lt;footer class=&#34;footer&#34;&gt;
</span><span style="color:#e6db74">            &lt;div class=&#34;container&#34;</span>&gt;
                <span style="color:#960050;background-color:#1e0010">@</span><span style="color:#a6e22e">v3u.cn</span>
            &lt;/<span style="color:#f92672">div</span>&gt;
        &lt;/<span style="color:#f92672">footer</span>&gt;
    &lt;/<span style="color:#f92672">div</span>&gt;
  
&lt;/<span style="color:#f92672">template</span>&gt;
&lt;<span style="color:#f92672">script</span>&gt;
<span style="color:#75715e">// 导入组件
</span><span style="color:#75715e"></span><span style="color:#66d9ef">import</span> <span style="color:#a6e22e">myheader</span> <span style="color:#a6e22e">from</span> <span style="color:#e6db74">&#39;./myheader.vue&#39;</span>
<span style="color:#75715e">// 导入文件
</span><span style="color:#75715e"></span><span style="color:#66d9ef">import</span> {<span style="color:#a6e22e">config</span>,<span style="color:#a6e22e">formatXml</span>} <span style="color:#a6e22e">from</span> <span style="color:#e6db74">&#39;../config.js&#39;</span>

<span style="color:#66d9ef">export</span> <span style="color:#66d9ef">default</span> {
    <span style="color:#a6e22e">data</span>(){
        <span style="color:#66d9ef">return</span>{
            <span style="color:#75715e">// 下拉框默认
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">selected</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;衣服&#39;</span>,
            <span style="color:#75715e">// 商品分类
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">category</span><span style="color:#f92672">:</span>[],
            <span style="color:#75715e">// 上传进度展示
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">load_percent</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>,
            <span style="color:#75715e">// 整型进度
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">load_int</span> <span style="color:#f92672">:</span> <span style="color:#ae81ff">0</span>,
            <span style="color:#75715e">// 视频链接
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">video</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>,
            <span style="color:#75715e">// 图片链接
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">src</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>, 
            <span style="color:#75715e">// 七牛云token
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">token</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>,
            <span style="color:#a6e22e">name</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>,
            <span style="color:#a6e22e">price</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>,
            <span style="color:#a6e22e">color</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>,
            <span style="color:#a6e22e">size</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>,
            <span style="color:#a6e22e">season</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>,
            <span style="color:#75715e">// 面包屑导航变量
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">datas</span><span style="color:#f92672">:</span>[{<span style="color:#a6e22e">title</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;首页&#39;</span>,<span style="color:#a6e22e">route</span><span style="color:#f92672">:</span>{<span style="color:#a6e22e">name</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;index&#39;</span>}},{<span style="color:#a6e22e">title</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;添加商品&#39;</span>}]
        }
    },
    <span style="color:#a6e22e">components</span><span style="color:#f92672">:</span>{
        <span style="color:#e6db74">&#39;myheader&#39;</span><span style="color:#f92672">:</span><span style="color:#a6e22e">myheader</span>,
    },
    <span style="color:#75715e">// 钩子
</span><span style="color:#75715e"></span>    <span style="color:#a6e22e">mounted</span><span style="color:#f92672">:</span><span style="color:#66d9ef">function</span>(){
        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">get_token</span>();
        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">get_category</span>();

    },
    <span style="color:#a6e22e">methods</span><span style="color:#f92672">:</span>{
        <span style="color:#75715e">// 获取商品分类数据
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">get_category</span><span style="color:#f92672">:</span><span style="color:#66d9ef">function</span>(){
            
            <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">axios</span>.<span style="color:#a6e22e">get</span>(<span style="color:#e6db74">&#39;http://127.0.0.1:8000/categorylist/&#39;</span>).<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">result</span>)=&gt;{
                
                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">result</span>);
                <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">mycate</span> <span style="color:#f92672">=</span> [];
                <span style="color:#75715e">// 二次处理数据
</span><span style="color:#75715e"></span>                <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">let</span> <span style="color:#a6e22e">i</span><span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>;<span style="color:#a6e22e">i</span>&lt;<span style="color:#f92672">result</span><span style="color:#a6e22e">.data.length</span><span style="color:#960050;background-color:#1e0010">;</span><span style="color:#a6e22e">i</span><span style="color:#960050;background-color:#1e0010">++)</span>{
                    <span style="color:#a6e22e">mycate.push</span><span style="color:#960050;background-color:#1e0010">(</span><span style="color:#a6e22e">result.data</span><span style="color:#960050;background-color:#1e0010">[</span><span style="color:#a6e22e">i</span><span style="color:#960050;background-color:#1e0010">][&#39;</span><span style="color:#a6e22e">name</span><span style="color:#960050;background-color:#1e0010">&#39;]);</span>
                }<span style="color:#960050;background-color:#1e0010">;</span>
                <span style="color:#960050;background-color:#1e0010">//</span> <span style="color:#a6e22e">赋值</span>
                <span style="color:#a6e22e">this.category</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#a6e22e">mycate</span><span style="color:#960050;background-color:#1e0010">;</span>
            }<span style="color:#960050;background-color:#1e0010">);</span>


        }<span style="color:#960050;background-color:#1e0010">,</span>
        <span style="color:#960050;background-color:#1e0010">//</span> <span style="color:#a6e22e">获取七牛云token</span>   
        <span style="color:#a6e22e">get_token</span><span style="color:#f92672">:</span><span style="color:#a6e22e">function</span><span style="color:#960050;background-color:#1e0010">()</span>{
            <span style="color:#960050;background-color:#1e0010">//</span> <span style="color:#a6e22e">请求后台接口</span>
            <span style="color:#a6e22e">this.axios.get</span><span style="color:#960050;background-color:#1e0010">(&#34;</span><span style="color:#a6e22e">http</span><span style="color:#f92672">:</span><span style="color:#960050;background-color:#1e0010">//</span><span style="color:#a6e22e">127.0.0.1</span><span style="color:#f92672">:</span><span style="color:#a6e22e">8000</span><span style="color:#960050;background-color:#1e0010">/</span><span style="color:#a6e22e">qiniu</span><span style="color:#960050;background-color:#1e0010">/&#34;)</span><span style="color:#a6e22e">.then</span><span style="color:#960050;background-color:#1e0010">((</span><span style="color:#a6e22e">result</span><span style="color:#960050;background-color:#1e0010">)=</span>&gt;{
                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">result</span>);
                <span style="color:#75715e">// 赋值给token
</span><span style="color:#75715e"></span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">token</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">result</span>.<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">token</span>;
                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">token</span>);
            });
        },
        <span style="color:#75715e">// 上传文件
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">upload_qiniu</span><span style="color:#f92672">:</span><span style="color:#66d9ef">function</span>(<span style="color:#a6e22e">e</span>){
            <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">size</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
            <span style="color:#75715e">// 获取文件
</span><span style="color:#75715e"></span>            <span style="color:#66d9ef">let</span> <span style="color:#a6e22e">file</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">target</span>.<span style="color:#a6e22e">files</span>[<span style="color:#ae81ff">0</span>];
            <span style="color:#a6e22e">size</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">size</span>;
            <span style="color:#a6e22e">size</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">size</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">1024</span>;
            <span style="color:#a6e22e">size</span> <span style="color:#f92672">=</span> (<span style="color:#a6e22e">size</span> <span style="color:#f92672">/</span> <span style="color:#ae81ff">1024</span>).<span style="color:#a6e22e">toFixed</span>(<span style="color:#ae81ff">3</span>);
            <span style="color:#75715e">// 声明表单参数
</span><span style="color:#75715e"></span>            <span style="color:#66d9ef">let</span> <span style="color:#a6e22e">param</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">FormData</span>();
            <span style="color:#75715e">// 文件添加到param
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">param</span>.<span style="color:#a6e22e">append</span>(<span style="color:#e6db74">&#39;file&#39;</span>,<span style="color:#a6e22e">file</span>,<span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">name</span>);
            <span style="color:#75715e">// 添加token
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">param</span>.<span style="color:#a6e22e">append</span>(<span style="color:#e6db74">&#39;token&#39;</span>,<span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">token</span>);
            <span style="color:#75715e">// 自定义axios
</span><span style="color:#75715e"></span>            <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">axios_qiniu</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">axios</span>.<span style="color:#a6e22e">create</span>({<span style="color:#a6e22e">withCredentials</span><span style="color:#f92672">:</span><span style="color:#66d9ef">false</span>});
            <span style="color:#75715e">// 发送请求
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">axios_qiniu</span>({
                <span style="color:#a6e22e">method</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;POST&#39;</span>,
                <span style="color:#a6e22e">url</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;http://up-z1.qiniu.com/&#39;</span>,
                <span style="color:#a6e22e">data</span><span style="color:#f92672">:</span><span style="color:#a6e22e">param</span>,
                <span style="color:#a6e22e">timeout</span><span style="color:#f92672">:</span><span style="color:#ae81ff">30000</span>,
                <span style="color:#75715e">// 上传过程中的方法
</span><span style="color:#75715e"></span>                <span style="color:#a6e22e">onUploadProgress</span><span style="color:#f92672">:</span>(<span style="color:#a6e22e">e</span>)=&gt;{
                    <span style="color:#75715e">// 计算上传百分比
</span><span style="color:#75715e"></span>                    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">complete</span> <span style="color:#f92672">=</span> (<span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">loaded</span> <span style="color:#f92672">/</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">total</span>);
                    <span style="color:#75715e">// 处理美化
</span><span style="color:#75715e"></span>                    <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">complete</span> <span style="color:#f92672">&lt;</span> <span style="color:#ae81ff">1</span>){
                        <span style="color:#75715e">// (complete为以完成的量 * 100).保留两位小数 + &#39;%&#39;
</span><span style="color:#75715e"></span>                        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">load_percent</span>  <span style="color:#f92672">=</span> (<span style="color:#a6e22e">complete</span> <span style="color:#f92672">*</span> <span style="color:#ae81ff">100</span>).<span style="color:#a6e22e">toFixed</span>(<span style="color:#ae81ff">2</span>) <span style="color:#f92672">+</span> <span style="color:#e6db74">&#39;%&#39;</span>;
                        <span style="color:#75715e">// 整型进度
</span><span style="color:#75715e"></span>                        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">load_int</span> <span style="color:#f92672">=</span> parseInt((<span style="color:#a6e22e">complete</span> <span style="color:#f92672">*</span> <span style="color:#ae81ff">100</span>).<span style="color:#a6e22e">toFixed</span>(<span style="color:#ae81ff">2</span>));
                    };
                }                
            }).<span style="color:#a6e22e">then</span>((<span style="color:#a6e22e">result</span>)=&gt;{
                <span style="color:#75715e">// 手动赋值%100(防止用户上传完成compltet还在计算)
</span><span style="color:#75715e"></span>                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">load_percent</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;100%&#39;</span>;
                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">load_int</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">100</span>;
                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">result</span>.<span style="color:#a6e22e">data</span>);
                <span style="color:#75715e">// 这里判断用户的文件是否大于4m
</span><span style="color:#75715e"></span>                <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">size</span>&lt;<span style="color:#f92672">4</span><span style="color:#960050;background-color:#1e0010">)</span>{
                    <span style="color:#a6e22e">this.src</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">http</span><span style="color:#f92672">:</span><span style="color:#960050;background-color:#1e0010">//</span><span style="color:#a6e22e">q9xyoh7sx.bkt.clouddn.com</span><span style="color:#960050;background-color:#1e0010">/&#39;</span> <span style="color:#960050;background-color:#1e0010">+</span> <span style="color:#a6e22e">result.data.key</span><span style="color:#960050;background-color:#1e0010">;</span>
                }<span style="color:#a6e22e">else</span>{
                    <span style="color:#a6e22e">this.video</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#960050;background-color:#1e0010">&#39;</span><span style="color:#a6e22e">http</span><span style="color:#f92672">:</span><span style="color:#960050;background-color:#1e0010">//</span><span style="color:#a6e22e">q9xyoh7sx.bkt.clouddn.com</span><span style="color:#960050;background-color:#1e0010">/&#39;</span> <span style="color:#960050;background-color:#1e0010">+</span> <span style="color:#a6e22e">result.data.key</span>
                }
            }<span style="color:#960050;background-color:#1e0010">)</span>
        }<span style="color:#960050;background-color:#1e0010">,</span>
        <span style="color:#960050;background-color:#1e0010">//</span> <span style="color:#a6e22e">添加商品</span>
        <span style="color:#a6e22e">submit</span><span style="color:#f92672">:</span><span style="color:#a6e22e">function</span><span style="color:#960050;background-color:#1e0010">()</span>{
            <span style="color:#a6e22e">console.log</span><span style="color:#960050;background-color:#1e0010">(</span><span style="color:#a6e22e">this.selected</span><span style="color:#960050;background-color:#1e0010">);</span>
            <span style="color:#960050;background-color:#1e0010">//</span> <span style="color:#a6e22e">将普通字段转换为json</span>
            <span style="color:#a6e22e">var</span> <span style="color:#a6e22e">param </span><span style="color:#f92672">=</span> {}<span style="color:#960050;background-color:#1e0010">;</span>

            <span style="color:#a6e22e">param</span><span style="color:#960050;background-color:#1e0010">[&#39;</span><span style="color:#a6e22e">color</span><span style="color:#960050;background-color:#1e0010">&#39;]</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#a6e22e">this.color</span><span style="color:#960050;background-color:#1e0010">;</span>
            <span style="color:#a6e22e">param</span><span style="color:#960050;background-color:#1e0010">[&#39;</span><span style="color:#a6e22e">size</span><span style="color:#960050;background-color:#1e0010">&#39;]</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#a6e22e">this.size</span><span style="color:#960050;background-color:#1e0010">;</span>
            <span style="color:#a6e22e">param</span><span style="color:#960050;background-color:#1e0010">[&#39;</span><span style="color:#a6e22e">season</span><span style="color:#960050;background-color:#1e0010">&#39;]</span> <span style="color:#960050;background-color:#1e0010">=</span> <span style="color:#a6e22e">this.season</span><span style="color:#960050;background-color:#1e0010">;</span>
            <span style="color:#960050;background-color:#1e0010">//</span> <span style="color:#a6e22e">转换为字符串</span>
            <span style="color:#a6e22e">param </span><span style="color:#f92672">=</span> <span style="color:#a6e22e">JSON.stringify</span><span style="color:#960050;background-color:#1e0010">(</span><span style="color:#a6e22e">param</span><span style="color:#960050;background-color:#1e0010">);</span>
            <span style="color:#a6e22e">console.log</span><span style="color:#960050;background-color:#1e0010">(</span><span style="color:#a6e22e">param</span><span style="color:#960050;background-color:#1e0010">);</span>
            <span style="color:#a6e22e">this.axios.get</span><span style="color:#960050;background-color:#1e0010">(&#39;</span><span style="color:#a6e22e">http</span><span style="color:#f92672">:</span><span style="color:#960050;background-color:#1e0010">//</span><span style="color:#a6e22e">127.0.0.1</span><span style="color:#f92672">:</span><span style="color:#a6e22e">8000</span><span style="color:#960050;background-color:#1e0010">/</span><span style="color:#a6e22e">insertgoods</span><span style="color:#960050;background-color:#1e0010">/&#39;,</span>{<span style="color:#a6e22e">params</span><span style="color:#f92672">:</span>{<span style="color:#a6e22e">name</span><span style="color:#f92672">:</span><span style="color:#a6e22e">this.name</span><span style="color:#960050;background-color:#1e0010">,</span><span style="color:#a6e22e">price</span><span style="color:#f92672">:</span><span style="color:#a6e22e">this.price</span><span style="color:#960050;background-color:#1e0010">,</span><span style="color:#a6e22e">img</span><span style="color:#f92672">:</span><span style="color:#a6e22e">this.src</span><span style="color:#960050;background-color:#1e0010">,</span><span style="color:#a6e22e">param</span><span style="color:#f92672">:</span><span style="color:#a6e22e">param</span><span style="color:#960050;background-color:#1e0010">,</span><span style="color:#a6e22e">selected</span><span style="color:#f92672">:</span><span style="color:#a6e22e">this.selected</span><span style="color:#960050;background-color:#1e0010">,</span><span style="color:#a6e22e">video</span><span style="color:#f92672">:</span><span style="color:#a6e22e">this.video</span>}}<span style="color:#960050;background-color:#1e0010">)</span><span style="color:#a6e22e">.then</span><span style="color:#960050;background-color:#1e0010">((</span><span style="color:#a6e22e">result</span><span style="color:#960050;background-color:#1e0010">)=</span>&gt;{

                <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">result</span>);

                <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">$Message</span>(<span style="color:#a6e22e">result</span>.<span style="color:#a6e22e">data</span>.<span style="color:#a6e22e">message</span>);
            })
        }     
    }
}
&lt;/<span style="color:#f92672">script</span>&gt;
</code></pre></div><p>这里需要注意的是，商品规格是为数组，需将数组拆分为对应字段供用户添加商品使用，当用户点击提交时，将拆分的数据转化为json格式，再将json转换为字符串进行存储</p>
<p>上传文件接口为七牛云上传文件接口，具体参照（七牛云进度条显示上传文件.md）</p>
<p>头部组件参照(实现模拟注册登录.md)进行复制</p>
<p>存储后的数据，建议测试json格式</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-json" data-lang="json">{<span style="color:#f92672">&#34;color&#34;</span>:<span style="color:#e6db74">&#34;玫瑰红&#34;</span>,<span style="color:#f92672">&#34;size&#34;</span>:<span style="color:#e6db74">&#34;长度为149.1mm，厚度为7.57mm&#34;</span>,<span style="color:#f92672">&#34;season&#34;</span>:<span style="color:#e6db74">&#34;&#34;</span>}
</code></pre></div>
  </div>

  <footer>
    <ul class="stats">
  <li class="categories">
    <ul>
        
            
            
                <i class="fa fa-folder"></i>
                
                
                <li><a class="article-category-link" href="/blog/blog/categories/hugo">Hugo</a></li>
                
            
        
    </ul>
  </li>
  <li class="tags">
    <ul>
        
            
            
                <i class="fa fa-tags"></i>
                
                
                <li><a class="article-category-link" href="/blog/blog/tags/tutorial">tutorial</a></li>
                
            
        
    </ul>
  </li>
</ul>

  </footer>

</article>

    <article class="post">
        <div id="disqus_thread"></div>
<script type="application/javascript">
    var disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "shortname" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
    </article>


<ul class="actions pagination">
    
        <li><a href="/blog/blog/vue%E6%8B%A6%E6%88%AA%E5%99%A8/"
                class="button big previous">vue路由拦截器</a></li>
    

    
        <li><a href="/blog/blog/%E5%89%8D%E7%AB%AF%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C/"
                class="button big next">前端文件上传</a></li>
    
</ul>


    </div>
    
<section id="sidebar">

  
  <section id="intro">
    
    
      
        <a href='/blog/'><img src="/blog/img/main/touxiang3.jpg" class="intro-circle" width="150px" alt="Hugo Future Imperfect" /></a>
      
    
    
      <header>
        <h2>mff</h2>
        <p>Effort is for money, and money is life!</p>
      </header>
    
    
      <ul class="icons">
        
          
    <li><a href="/blog/blog/index.xml" type="application/rss+xml" target="_blank" title="RSS" class="fa fa-rss"></a></li>


        
        












































































  <li><a href="mailto:2980738482@qq.com" title="Email" class="fa fa-envelope"></a></li>


      </ul>
    
  </section>

  
  <section class="recent-posts">
    <div class="mini-posts">
      <header>
        <h3>Recent Posts</h3>
      </header>
      <div class="posts-container">
        

        
          
        

        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/fastdfs%E9%85%8D%E7%BD%AE%E5%8F%8A%E4%BB%8B%E7%BB%8D/">FastDFS配置及介绍</a>
              </h3>
              
              <time class="published" datetime='2020-08-27'>
                August 27, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/orm%E5%B8%B8%E8%A7%84%E6%93%8D%E4%BD%9C%E9%87%8D%E7%82%B9/">orm常规操作</a>
              </h3>
              
              <time class="published" datetime='2020-07-28'>
                July 28, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/%E6%94%AF%E4%BB%98%E5%AE%9D%E6%B2%99%E7%AE%B1%E6%94%AF%E4%BB%98%E6%93%8D%E4%BD%9C/">支付宝沙箱支付操作</a>
              </h3>
              
              <time class="published" datetime='2020-07-28'>
                July 28, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/%E8%85%BE%E8%AE%AF%E7%9F%AD%E4%BF%A1%E5%8F%91%E9%80%81%E9%AA%8C%E8%AF%81%E7%A0%81/">腾讯短信发送验证码</a>
              </h3>
              
              <time class="published" datetime='2020-07-28'>
                July 28, 2020
              </time>
            </header>
            

          </article>
        
          <article class="mini-post">
            <header>
              <h3>
                <a href="/blog/blog/flask&#43;vue%E5%AE%9E%E7%8E%B0%E5%89%8D%E5%90%8E%E7%AB%AF%E5%88%86%E7%A6%BB/">flask&#43;vue实现前后端分离</a>
              </h3>
              
              <time class="published" datetime='2020-07-26'>
                July 26, 2020
              </time>
            </header>
            

          </article>
        
      </div>

      
        <a href=
          
            /blog/
          
        class="button">View more posts</a>
      
    </div>
  </section>

  
  
  
  
  
    <section id="categories">
      <header>
        <h3>
          <a href="/blog/categories/">Categories</a>
        </h3>
      </header>
        
          
        

        
        <p>
          <article>
            <header>
              
                <a href="/blog/categories/hugo/">hugo</a>
                <span style="float:right;">43</span>
              
            </header>
          </article>
        </p>
        
    </section>
  
  

  
  
    <section id="mini-bio">
      <h3>About</h3>
      <p>blog于2018年6月10日创建(It was created on May 10, 2020),文章主要介绍了python的一些常用框架和库及它们的一些用法</p>
      <a href="/blog/about/" class="button">Learn More</a>
    </section>
  

  
  <section id="footer">
    
      <ul class="icons">
        
          
    <li><a href="/blog/blog/index.xml" type="application/rss+xml" target="_blank" title="RSS" class="fa fa-rss"></a></li>


        
        












































































  <li><a href="mailto:2980738482@qq.com" title="Email" class="fa fa-envelope"></a></li>


      </ul>
    
    <p class="copyright">
      
        &copy; 2020
        
          mff blog
        
      .
      Powered by <a href="//gohugo.io" target="_blank">Hugo</a>
    </p>
  </section>
</section>

    </div>
    <a id="back-to-top" href="#" class="fa fa-arrow-up fa-border fa-2x"></a>
    

    
      
    

    
      
      
      
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/highlight.min.js"></script>
        
        
        
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/languages/r.min.js"></script>
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/languages/yaml.min.js"></script>
        <script src="//cdn.bootcss.com/highlight.js/9.11.0/languages/css.min.js"></script>
        <script>hljs.configure({languages: []}); hljs.initHighlightingOnLoad();</script>
      
    
    
    
      <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/skel/3.0.1/skel.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.js"></script>
      <script src="/blog/js/util.js"></script>
      <script src="/blog/js/main.js"></script>
      <script src="/blog/js/backToTop.js"></script>
    

    
      
        
      
    

    
    <script>hljs.initHighlightingOnLoad();</script>
      <script src="//yihui.name/js/math-code.js"></script>
<script async
src="//cdn.bootcss.com/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML">
</script>


  </body>
</html>

